package com.hskn.hss.module.tlfacilities.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.file.FileTypeUtils;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.exception.HssException;

import com.hskn.hss.module.common.ienum.ExcelTypeEnum;
import com.hskn.hss.module.elementtotal.vo.ElementsListByAppVo;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.tlempincidental.mapper.TlEmpIncidentalMapper;
import com.hskn.hss.module.tlequipmodel.entity.TlEquipmodel;
import com.hskn.hss.module.tlfacilities.entity.TIFacilitiesParkVO;
import com.hskn.hss.module.tlfacilities.entity.TIFacilitiesTreeVO;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.entity.TlFacilitiesExcelInput;
import com.hskn.hss.module.tlfacilities.iEnum.TIFacilitiesTypeEnum;
import com.hskn.hss.module.tlfacilities.mapper.TlFacilitiesMapper;
import com.hskn.hss.module.tlfacilities.service.ITlFacilitiesService;
import com.hskn.hss.module.tlfacilities.to.TIFacilitiesExcelReaderTO;
import com.hskn.hss.module.tlfacilities.to.TIFacilitiesExcelWriteTo;
import com.hskn.hss.module.tlfacilities.to.TIFacilitiesTo;

import com.hskn.hss.module.tlfacilities.vo.*;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.service.IUserService;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-13
 */
@Api(tags = {"-tl_facilities"}, description = "设施档案")
@Slf4j
@RestController
@RequestMapping("/hss/tlfacilities")
public class TlFacilitiesController {

    @Autowired
    ITlFacilitiesService tlFacilitiesService;
    @Resource
    private TlFacilitiesMapper tlFacilitiesMapper;
    @Autowired
    private IUserService userService;

    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_facilities", bean = TlFacilities.class, beanMapper = TlFacilitiesMapper.class, names = "faclname")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(@RequestBody @Validated TlFacilities tlFacilities, HttpServletRequest request) {
        try {
            QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFaclname, tlFacilities.getFaclname());
            List<TlFacilities> facilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
            if (facilitiesList.isEmpty()) {
                tlFacilitiesService.insert(tlFacilities);
                return AjaxResult.success(tlFacilities);
            } else {
                return AjaxResult.fail("该名称已存在，请重新输入");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_facilities", idKey = "id", bean = TlFacilities.class, beanMapper = TlFacilitiesMapper.class, names = "faclname")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return tlFacilitiesService.update(params);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_facilities", idKey = "ids", bean = TlFacilities.class, beanMapper = TlFacilitiesMapper.class, names = "faclname")
    @RequestMapping(value = "updatebyids", method = RequestMethod.POST)
    public AjaxResult<Object> updatebyids(@RequestBody @Validated TlFacilities tlFacilities ,HttpServletRequest request) {
        try {
            return tlFacilitiesService.updatebyids(tlFacilities);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_facilities", idKey = "id", bean = TlFacilities.class, beanMapper = TlFacilitiesMapper.class, names = "faclname")
    @RequestMapping(value = "deletebyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return tlFacilitiesService.deletebyid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_facilities", idKey = "ids", bean = TlFacilities.class, beanMapper = TlFacilitiesMapper.class, names = "faclname")
    @RequestMapping(value = "deletebatchbyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebatchbyid(@RequestBody @Validated TlFacilities tlFacilities) {

        try {
            return tlFacilitiesService.deletebatchbyid(tlFacilities);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "getSelectList", method = RequestMethod.POST)
    public AjaxResult<Object> getSelectList(HttpServletRequest request) {
        return AjaxResult.success(tlFacilitiesService.getSelectList());
    }

    /**
     * 树木科属种类树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getTreeSpec", method = RequestMethod.POST)
    public AjaxResult<Object> getTreeSpec(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        String id = null;
        if (params != null && params.containsKey("id")) {
            id = params.getString("id");
        } else {
            return AjaxResult.fail("参数不存在");
        }
        return AjaxResult.success(tlFacilitiesService.getTreeSpec(id));

    }

    @RequestMapping(value = "getDetail", method = RequestMethod.POST)
    public AjaxResult<Object> getDetail(@RequestBody @Validated TlFacilities tlFacilities) {

        TlFacilities ttlFacilities = null;
        try {
            ttlFacilities = tlFacilitiesService.getDetail(tlFacilities);
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(ttlFacilities);

    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "selectLjpyWorkPlan", method = RequestMethod.POST)
    public AjaxResult<Object> selectLjpyWorkPlan(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        List<RptCarWork> tlFacilities = null;
        String id = "";
        if (params != null && params.containsKey("id")) {
            id = params.getString("id");
        } else {
            return AjaxResult.fail("参数不存在");
        }
        tlFacilities = tlFacilitiesService.selectLjpyWorkPlan(id);
        return AjaxResult.success(tlFacilities);

    }

    @RequestMapping(value = "getTreeSpecList", method = RequestMethod.POST)
    public AjaxResult<Object> getTreeSpecList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        Map<String, Object> map = new HashMap();
        String id = "";
        if (params != null && params.containsKey("id")) {
            id = params.getString("id");
        } else {
            return AjaxResult.fail("参数不存在");
        }
        map = tlFacilitiesService.getTreeSpecList(id);
        return AjaxResult.success(map);

    }

    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public AjaxResult<Object> getList(@RequestBody @Validated TIFacilitiesTo tiFacilitiesTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(tlFacilitiesService.getList(entity, tiFacilitiesTo));
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getSelectListByuid", method = RequestMethod.POST)
    public AjaxResult<Object> getSelectListByuid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return AjaxResult.success(tlFacilitiesService.getSelectListByuid(params));
    }

    @RequestMapping(value = "getParamTree", method = RequestMethod.POST)
    public AjaxResult<Object> getParamTree(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<TreeEntity> paramTree = tlFacilitiesService.getParamTree(params);
            return AjaxResult.success(paramTree);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

    /**
     * 考勤日历
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "selectCarWorkByDate", method = RequestMethod.POST)
    public AjaxResult<Object> selectCarWorkByDate(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<JSONObject> paramTree = tlFacilitiesService.selectCarWorkByDate(params);
            return AjaxResult.success(paramTree);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

    /**
     * 厕所设备人员信息
     *
     * @param request
     * @return
     */

    @RequestMapping(value = "selectTlequipByFacId", method = RequestMethod.POST)
    public AjaxResult<Object> selectTlequipByFacId(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject paramTree = tlFacilitiesService.selectTlequipByFacId(params);
            return AjaxResult.success(paramTree);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

    /**
     * 获取列表
     *
     * @param request
     * @return
     */
//    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_facilities", bean = TlFacilities.class, beanMapper = TlFacilitiesMapper.class, names = "faclname")
    @ApiOperation(value = "设施档案导入接口", notes = "设施档案导入")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @PostMapping("excelReader")
    public AjaxResult excelReader(HttpServletRequest request,@ModelAttribute @Validated TIFacilitiesExcelReaderTO excelReaderTo) {
        try {

            MultipartFile file = excelReaderTo.getFiles();
            String types = excelReaderTo.getTypes();
            if (Arrays.stream(ExcelTypeEnum.values()).noneMatch(s -> s.getName().equals(FileTypeUtils.getFileType(file.getOriginalFilename().toLowerCase())))) {
                return AjaxResult.fail("导入文件格式错误");
            }
            InputStream inputStream = new BufferedInputStream(file.getInputStream());
            List<TlFacilities> tlFacilitiesList = new ArrayList<>();

            if (Arrays.stream(TIFacilitiesTypeEnum.values())
                    .anyMatch(s -> s.getName().equals(types))) {
                if (types.equals("设施")) {
                    ExcelUtil<TlFacilities> util = new ExcelUtil<TlFacilities>(TlFacilities.class);
                    tlFacilitiesList = util.importExcel(inputStream);
                } else if (types.equals("园林")) {
                    ExcelUtil<TIFacilitiesParkVO> util = new ExcelUtil<TIFacilitiesParkVO>(TIFacilitiesParkVO.class);
                    List<TIFacilitiesParkVO> tiFacilitiesParkVOList = util.importExcel(inputStream);
                    tlFacilitiesList = JSONObject.parseArray(JSONObject.toJSONString(tiFacilitiesParkVOList), TlFacilities.class);
                } else if (types.equals("古树")) {
                    ExcelUtil<TIFacilitiesTreeVO> util = new ExcelUtil<TIFacilitiesTreeVO>(TIFacilitiesTreeVO.class);
                    List<TIFacilitiesTreeVO> tiFacilitiesTreeVOList = util.importExcel(inputStream);
                    tlFacilitiesList = JSONObject.parseArray(JSONObject.toJSONString(tiFacilitiesTreeVOList), TlFacilities.class);
                }

            }else {
                return AjaxResult.fail("类型错误");
            }
            if (tlFacilitiesList.isEmpty()) {
                return AjaxResult.fail("导入内容为空");
            }
            tlFacilitiesService.excelReader(tlFacilitiesList);
            if (tlFacilitiesService.saveBatch(tlFacilitiesList)) {
                return AjaxResult.success("导入成功，请在系统中及时填写设施地址，否则在系统中无法显示");
            } else {
                return AjaxResult.fail("导入失败");
            }
        } catch (ExcelReaderDataException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.fail();
        }

    }
        @ApiOperation(value = "设施档案导出接口", notes = "设施档案导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesExcelOutVO.class)
    })
    @PostMapping("excelExport")
    @RequestMapping(value = "excelExport", method = RequestMethod.POST)
    public com.hskn.hss.core.domain.AjaxResult excelExport(HttpServletRequest request, @RequestBody @Validated TIFacilitiesTo tiFacilitiesTo) {
        try {
            if((tiFacilitiesTo.getIdlist()!=null&&tiFacilitiesTo.getIdlist().isEmpty())){
                return com.hskn.hss.core.domain.AjaxResult.error("请选择至少一条数据导出");
            }
            QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclname())) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFaclname, tiFacilitiesTo.getFaclname());
            }
            if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacltypeid())) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, tiFacilitiesTo.getFacltypeid());
            }
            if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacltypeitemid())) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeitemid, tiFacilitiesTo.getFacltypeitemid());
            }
            if (StringUtils.isNotEmpty(tiFacilitiesTo.getMtadeptId())) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getMtadeptid, tiFacilitiesTo.getMtadeptId());
            }
            if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclstatus())) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFaclstatus, tiFacilitiesTo.getFaclstatus());
            }
            if(tiFacilitiesTo.getIdlist()!=null&&!tiFacilitiesTo.getIdlist().isEmpty()){
                tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getId,tiFacilitiesTo.getIdlist());
            }
            String idsStr = tiFacilitiesTo.getIds();
            if (StringUtils.isNotEmpty(idsStr)) {
                String[] ids = idsStr.split(",");
                if (StringUtils.isNotEmpty(ids) && ids.length > 0) {
                    tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getId, ids);
                }
            }
            String excludeIdsStr = tiFacilitiesTo.getExcludeIds();
            if (StringUtils.isNotEmpty(excludeIdsStr)) {
                String[] excludeIds = excludeIdsStr.split(",");
                if (StringUtils.isNotEmpty(excludeIds) && excludeIds.length > 0) {
                    tlFacilitiesQueryWrapper.lambda().notIn(TlFacilities::getId, excludeIds);
                }
            }
            List<String> list = new ArrayList<>();

            if (tiFacilitiesTo.getType().equals("园林")) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "ad120d17c4fd2bb5d88bb48cb39e4f38");
            } else if (tiFacilitiesTo.getType().equals("古树")) {
                tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "c9fb46f67c21cf0a9068260a5911b303");
            } else {
                list.add("ad120d17c4fd2bb5d88bb48cb39e4f38");
                list.add("c9fb46f67c21cf0a9068260a5911b303");
                tlFacilitiesQueryWrapper.lambda().notIn(TlFacilities::getFacltypeid, list);
            }
            List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
            if (tlFacilitiesList.isEmpty()) {
                return com.hskn.hss.core.domain.AjaxResult.error("导出数据为空");
            }
            if (tiFacilitiesTo.getType().equals("园林")) {
                ExcelUtil<TIFacilitiesParkExcelOutVO> util = new ExcelUtil<TIFacilitiesParkExcelOutVO>(TIFacilitiesParkExcelOutVO.class);
                List<TIFacilitiesParkExcelOutVO> TIFacilitiesParkExcelOutVOList = JSONObject.parseArray(JSONObject.toJSONString(tlFacilitiesList), TIFacilitiesParkExcelOutVO.class);
                Map<String, User> userMap = userService.getUserMap();
                for (TIFacilitiesParkExcelOutVO TIFacilitiesParkExcelOutVO : TIFacilitiesParkExcelOutVOList) {
                    if (userMap.containsKey(TIFacilitiesParkExcelOutVO.getPrincipal())) {
                        TIFacilitiesParkExcelOutVO.setPrincipalname(userMap.get(TIFacilitiesParkExcelOutVO.getPrincipal()).getName());
                    }
                }
                return util.exportExcel(TIFacilitiesParkExcelOutVOList, "园林档案");
            } else if (tiFacilitiesTo.getType().equals("古树")) {
                ExcelUtil<TIFacilitiesTreeExcelOutVO> util = new ExcelUtil<TIFacilitiesTreeExcelOutVO>(TIFacilitiesTreeExcelOutVO.class);
                List<TIFacilitiesTreeExcelOutVO> TIFacilitiesTreeExcelOutVOList = JSONObject.parseArray(JSONObject.toJSONString(tlFacilitiesList), TIFacilitiesTreeExcelOutVO.class);
                Map<String, User> userMap = userService.getUserMap();
                for (TIFacilitiesTreeExcelOutVO TIFacilitiesTreeExcelOutVO : TIFacilitiesTreeExcelOutVOList) {
                    if (userMap.containsKey(TIFacilitiesTreeExcelOutVO.getPrincipal())) {
                        TIFacilitiesTreeExcelOutVO.setPrincipalname(userMap.get(TIFacilitiesTreeExcelOutVO.getPrincipal()).getName());
                    }
                }
                return util.exportExcel(TIFacilitiesTreeExcelOutVOList, "古树档案");
            } else {
                ExcelUtil<TIFacilitiesExcelOutVO> util = new ExcelUtil<TIFacilitiesExcelOutVO>(TIFacilitiesExcelOutVO.class);
                List<TIFacilitiesExcelOutVO> TIFacilitiesExcelOutVOList = JSONObject.parseArray(JSONObject.toJSONString(tlFacilitiesList), TIFacilitiesExcelOutVO.class);
                Map<String, User> userMap = userService.getUserMap();
                for (TIFacilitiesExcelOutVO TIFacilitiesExcelOutVO : TIFacilitiesExcelOutVOList) {
                    if (userMap.containsKey(TIFacilitiesExcelOutVO.getPrincipal())) {
                        TIFacilitiesExcelOutVO.setPrincipalname(userMap.get(TIFacilitiesExcelOutVO.getPrincipal()).getName());
                    }

                }
                return util.exportExcel(TIFacilitiesExcelOutVOList, "设施档案");
            }
           /*

            TIFacilitiesVO tiFacilitiesVO = tlFacilitiesService.getList(tiFacilitiesTo);
            QueryWrapper<TlFacilities> tiFacilitiesQueryWrapper=new QueryWrapper<>();

            if(tiFacilitiesVO.getTlFacilitiesList().isEmpty()){
                return com.hskn.hss.core.domain.AjaxResult.error("导出数据为空");
            }
            ExcelUtil<TIFacilitiesExcelOutVO> util = new ExcelUtil<TIFacilitiesExcelOutVO>(TIFacilitiesExcelOutVO.class);
            List<TIFacilitiesExcelOutVO> TIFacilitiesExcelOutVOList=JSONObject.parseArray(JSONObject.toJSONString(tiFacilitiesVO.getTlFacilitiesList()),TIFacilitiesExcelOutVO.class);
            Map<String, User> userMap=userService.getUserMap();
            for(TIFacilitiesExcelOutVO TIFacilitiesExcelOutVO:TIFacilitiesExcelOutVOList){
                if(userMap.containsKey(TIFacilitiesExcelOutVO.getPrincipal())){
                    TIFacilitiesExcelOutVO.setName(userMap.get(TIFacilitiesExcelOutVO.getPrincipal()).getName());
                }

            }*/

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }

    @ApiOperation(value = "设施档案导出模板接口", notes = "设施档案模板导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesExcelOutVO.class)
    })
    @PostMapping("excelExportTemplate")
    public com.hskn.hss.core.domain.AjaxResult excelExportTemplate(HttpServletRequest request, @RequestBody @Validated TIFacilitiesTo tiFacilitiesTo) {
        try {
            if (tiFacilitiesTo.getType().equals("古树")) {
                //工具类实例化
                ExcelUtil<TIFacilitiesTreeVO> util = new ExcelUtil<TIFacilitiesTreeVO>(TIFacilitiesTreeVO.class);
                //数据载体转换
                List<TIFacilitiesTreeVO> tiFacilitiesTreeExcelOutVOList = new ArrayList<>();
                //导出
                return util.exportExcel(tiFacilitiesTreeExcelOutVOList, "古树档案模板");
            }else  if (tiFacilitiesTo.getType().equals("园林")){
                //工具类实例化
                ExcelUtil<TIFacilitiesParkVO> util = new ExcelUtil<TIFacilitiesParkVO>(TIFacilitiesParkVO.class);
                //数据载体转换
                List<TIFacilitiesParkVO> tiFacilitiesParkExcelOutVOList = new ArrayList<>();
                //导出
                return util.exportExcel(tiFacilitiesParkExcelOutVOList, "园林档案模板");
            }else{
                //工具类实例化
                ExcelUtil<TlFacilitiesExcelInput> util = new ExcelUtil<TlFacilitiesExcelInput>(TlFacilitiesExcelInput.class);
                //数据载体转换
                List<TlFacilitiesExcelInput> tiFacilitiesExcelOutVOList = new ArrayList<>();
                //导出
                return util.exportExcel(tiFacilitiesExcelOutVOList, "设施档案模板");
            }


        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    @ApiOperation(value = "公厕人流量统计接口", notes = "公厕人流量统计")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesTo.class)
    })
    @RequestMapping(value = "getToiletPerson", method = RequestMethod.POST)
    public AjaxResult<Object> getToiletPerson(@RequestBody @Validated TIFacilitiesTo tiFacilitiesTo) {
        try {
            return AjaxResult.success(tlFacilitiesService.getToiletPerson(tiFacilitiesTo));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }

    @ApiOperation(value = "公厕人流量统计图接口", notes = "公厕人流量统计图")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesTo.class)
    })
    @RequestMapping(value = "getToiletPersonZX", method = RequestMethod.POST)
    public AjaxResult<Object> getToiletPersonZX(@RequestBody @Validated TIFacilitiesTo tiFacilitiesTo) {
        try {
            return AjaxResult.success(tlFacilitiesService.getToiletPersonZX(tiFacilitiesTo));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }
    @ApiOperation(value = "公厕人流量统计明细接口", notes = "公厕人流量统计明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesTo.class)
    })
    @RequestMapping(value = "getToiletPersonMX", method = RequestMethod.POST)
    public AjaxResult<Object> getToiletPersonMX(@RequestBody @Validated TIFacilitiesTo tiFacilitiesTo) {
        try {
            return AjaxResult.success(tlFacilitiesService.getToiletPersonMX(tiFacilitiesTo));
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }

    /**
     * 公厕人流量统计导出接口
     * @param tiFacilitiesTo
     * @return
     */
    @ApiOperation(value = "公厕人流量统计导出接口", notes = "公厕人流量统计导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesTo.class)
    })
    @RequestMapping(value = "getToiletPersonExcel", method = RequestMethod.POST)
    public com.hskn.hss.core.domain.AjaxResult getToiletPersonExcel(@RequestBody @Validated TIFacilitiesExcelWriteTo tiFacilitiesTo) {
        try {
            if(tiFacilitiesTo.getIdlist() == null || tiFacilitiesTo.getIdlist().size()>0){
            List<TlFacilitiesExcelVO> oilDeviceServiceVOList =
                    JSONArray.parseArray(JSONObject.toJSONString(tlFacilitiesService.getToiletPersonExcelWrite(tiFacilitiesTo)), TlFacilitiesExcelVO.class);
            //工具类实例化
            ExcelUtil<TlFacilitiesExcelVO> util = new ExcelUtil<>(TlFacilitiesExcelVO.class);
            //导出
            return util.exportExcel(oilDeviceServiceVOList, "公厕人流量统计");
            } else{
                return com.hskn.hss.core.domain.AjaxResult.error("请选择至少一条数据导出");
            }
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }

    /**
     * 公厕人流量统计明细导出接口
     * @param tiFacilitiesTo
     * @return
     */
    @ApiOperation(value = "公厕人流量统计明细导出接口", notes = "公厕人流量统计明细导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TIFacilitiesTo.class)
    })
    @RequestMapping(value = "getToiletPersonMXExcel", method = RequestMethod.POST)
    public com.hskn.hss.core.domain.AjaxResult getToiletPersonMXExcel(@RequestBody @Validated TIFacilitiesExcelWriteTo tiFacilitiesTo) {
        try {
            if(tiFacilitiesTo.getToiletpersondate() == null || tiFacilitiesTo.getToiletpersondate().size()>0){
            List<TlFacilitiesExceldtVO> oilDeviceServiceVOList =
                    JSONArray.parseArray(JSONObject.toJSONString(tlFacilitiesService.getToiletPersonMXExcel(tiFacilitiesTo)), TlFacilitiesExceldtVO.class);
            //工具类实例化
            ExcelUtil<TlFacilitiesExceldtVO> util = new ExcelUtil<>(TlFacilitiesExceldtVO.class);
            //导出
            return util.exportExcel(oilDeviceServiceVOList, "公厕人流量统计");
            } else {
                return com.hskn.hss.core.domain.AjaxResult.error("请选择至少一条数据导出");
            }
        } catch (HssException | ParseException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }


    /**
     * APP首页设施数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "appHomeFacilities", method = RequestMethod.POST)
    public AjaxResult<Object> appHomeFacilities(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<ElementsListByAppVo> json = tlFacilitiesService.appHomeFacilities(params);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }
    /**
     * APP园林古树人员对应查询
     */
    @RequestMapping(value = "appGardensTree", method = RequestMethod.POST)
    public AjaxResult<Object> appGardensTree(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<TIFacilitiesAppTreeVO> tiFacilitiesAppTreeVOList = tlFacilitiesService.getTreeName(params);
            return AjaxResult.success(tiFacilitiesAppTreeVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

    /**
     * APP用户绑定档案类型查询
     */
    @RequestMapping(value = "appArchivesTree", method = RequestMethod.POST)
    public AjaxResult<Object> appArchivesTree(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<TIFacilitiesAppIdTreeVO> list = tlFacilitiesService.getArchivesTree(params);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

}

